// pages/chooseServer/chooserServer.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    classify: "",
    list: [],
    i: 0,
    key: 0,
    index: 0,
    site: 0,
    classifyList: ["设备安装", "系统维修", "系统调试", "设备安装"],
    classifySon: ["视频监控", "防盗报警", "门禁一卡通", "背景音乐", "综合网络", "停车场", "巡更", "机房"]
  },
  // 数据内容
  request: function () {
    var that = this;
    wx.request({
         url: 'https://www.xiaozheng8.com/service/cate/findAll',
        //  url: 'http://192.168.43.244:8002/service/cate/findAll',
         method: 'post',
      success: function (res) {
        console.log(res);
        that.setData({
          list: res.data.data,
          classifyList: res.data.data.sonList,
          // classifySon:res.data.data
        });
        console.log(that.data.classifyList);
        that.setData({
          classifyList: that.data.classifyList,
          nameList: that.data.classifyList[0]
        })
        console.log(that.data.nameList);
        that.colorChange();


      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  // 点击是否有内容
  colorChange:function(){
    var that=this;
    if (that.data.classifyList[that.data.i].sonList == "") {
      console.log(that.data.classifyList[that.data.i].sonList);
    } else {

      for (var i = 0; i < that.data.classifyList[that.data.i].sonList.length; i++) {
        that.data.classifyList[that.data.i].sonList[i].exist = false;
        if (that.data.classifyList[that.data.i].sonList[i].sonList.length > 0) {
          that.data.classifyList[that.data.i].sonList[i].exist = true;
          that.setData({
            classifyList: that.data.classifyList
          });
          console.log(that.data.classifyList)
        }
      }
    }
  },
  //点击大类
  checkBig: function (e) {
    var that = this;
    var check = e.currentTarget.dataset.key;
    // that.setData({


    // })
    // for (var i = 0; i < that.data.classifyList[check].length; i++) {
    if (that.data.classifyList[check].sonList.length == 0) {
      wx.showModal({
        title: '提示',
        content: '当前服务暂未开放',
        showCancel: false
      })
    } else {
      that.setData({
        key: check,
        i: check,
        nameList: that.data.classifyList[check]
      })
      that.colorChange();
      // }
    }


  },
  // 子类点击
  son: function (e) {
    var that = this;
    var cateId = e.currentTarget.dataset.cateid;
    var index = e.currentTarget.dataset.index;
    var check = e.currentTarget.dataset.check;
    this.setData({
      index: index,
      check: check
    })
    console.log(index);
    that.data.classifyList[that.data.i].sonList[index].check = !that.data.classifyList[that.data.i].sonList[index].check;
    var indexList = []
    // for (var i = 0; i < that.data.classifyList[that.data.i].sonList.length;i++){
    //   console.log(that.data.classifyList[that.data.i].sonList[i].check);
    // if (that.data.classifyList[that.data.i].sonList[i].check == true) {
    //   indexList.push(i);
    //   console.log(i);
    // }
    // var list = JSON.stringify(indexList)
    // that.setData({
    //   indexList: list
    // })
    // }

    that.setData({
      cateId: cateId,
      index: index,
      classifyList: that.data.classifyList
    })
    // 跳转至下一页
    if (that.data.classifyList[that.data.i].sonList[index].sonList.length == 0) {
      wx.showModal({
        title: '提示',
        content: '该服务内容暂未开放',
        showCancel: false
      })
    } else {
      wx.navigateTo({
        url: '../chooseContent/chooseContent?cateId=' + that.data.cateId + "&index=" + that.data.key + "&id=" + that.data.index,
      })
    }

  },
  // 跳转到下一页
  // next: function () {
  //   var that = this;

  //     success: function (res) { },
  //     fail: function (res) { },
  //     complete: function (res) { },
  //   })
  // },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    console.log(options.index);
    var that = this;
    if (options.index == undefined) { } else {
      console.log(options.index);
      that.setData({
        key: options.index
      })
      that.setData({
        i: options.index
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.request();
    // var that=this;

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})